package com.aaa.filter.filter;

import com.aaa.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@WebFilter("/*")
public class TitalsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req= (HttpServletRequest) request;
        HttpServletResponse rep= (HttpServletResponse) response;

        String requestURI = req.getRequestURI();
        if (requestURI.equals("/login")){
            chain.doFilter(request,response);
            log.info("执行了");
            return;
        }
        String tok=req.getHeader("token");
        if (tok == null || "".equals(tok)){
//            返回401
            rep.setStatus(401);
            return;
        }
        try {
            JwtUtils.parseJWT(tok);
        }catch (Exception e){
            e.printStackTrace();
            rep.setStatus(401);
            return;
        }
        chain.doFilter(request,response);
    }
}
